package org.example.minebackend.service.impl;

import org.example.minebackend.mapper.HealthClubMapper;
import org.example.minebackend.pojo.*;
import org.example.minebackend.service.HealthClubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HealthClubServiceImpl implements HealthClubService {
    @Autowired
    HealthClubMapper healthClubMapper;

    public DetailOfRecipe getRecipeDetails(Integer id) {
        DetailOfRecipe detailOfRecipe = new DetailOfRecipe();
        Recipe recipe = healthClubMapper.getRecipeById(id);
        detailOfRecipe.setId(recipe.getId());
        detailOfRecipe.setName(recipe.getName());
        detailOfRecipe.setNOfCollections(recipe.getNOfCollections());
        detailOfRecipe.setDisplayImage(recipe.getDisplayImage());
        List<IngredientOfRecipe> ingredients = healthClubMapper.getIngredientsByRecipeId(id);
        detailOfRecipe.setIngredients(ingredients);
        List<Step> steps = healthClubMapper.getStepsById(id);
        detailOfRecipe.setSteps(steps);
        return detailOfRecipe;
    }

    public List<Herb> getHerbsByBody(String body) {
        List<Herb> herbs = healthClubMapper.getHerbsByBody(body);
        return herbs;
    }

    public int addRecipeCollection(Integer userId, Integer recipeId) {
        // 先判断该用户是否收藏过
        int n = healthClubMapper.judgeCollection(userId, recipeId);
        if( n == 0 ) {
            // 先给recipeId的nOfCollection 进行 + 1
            healthClubMapper.addRecipeCollectionById(recipeId);

            // 将收藏插入表recipesOfCollection中
            healthClubMapper.insertRecipeCollectionById(userId, recipeId);
        }
        return n;
    }

    public int judgeCollection(Integer userId, Integer recipeId) {
        return healthClubMapper.judgeCollection(userId, recipeId);
    }

    public int deleteCollection(Integer userId, Integer recipeId) {
        int n = healthClubMapper.judgeCollection(userId, recipeId);
        if( n == 1 ) {
            healthClubMapper.deleteRecipeCollectionById(recipeId);
            healthClubMapper.deleteRecipeCollectionByTwoId(userId, recipeId);
        }
        return n;
    }

    public List<RecipeEntry> getRecipeEntry(String body) {
        List<Recipe> recipes = healthClubMapper.getRecipeByBody(body);
        List<RecipeEntry> recipeEntryList = new ArrayList<>();
        for (Recipe recipe : recipes) {
            Integer id = recipe.getId();
            List<IngredientOfRecipe> ingredients = healthClubMapper.getIngredientsByRecipeId(id);
            // 对Recipe以及它的Ingredinet进行处理，得到RecipeEntry
            RecipeEntry recipeEntry = getEntryByRecipeAndIngredients(recipe, ingredients);
            recipeEntryList.add(recipeEntry);
        }
        return recipeEntryList;
    }


    public static RecipeEntry getEntryByRecipeAndIngredients(Recipe recipe, List<IngredientOfRecipe> ingredients) {
        RecipeEntry recipeEntry = new RecipeEntry();
        recipeEntry.setId(recipe.getId());
        recipeEntry.setName(recipe.getName());
        recipeEntry.setDisplayImage(recipe.getDisplayImage());
        recipeEntry.setNOfCollections(recipe.getNOfCollections());
        StringBuilder temp = new StringBuilder("用料：");
        for (int i = 0; i < ingredients.size(); i++) {
            temp.append(ingredients.get(i).getIngredient());
            if (i != ingredients.size() - 1) {
                temp.append(", ");
            }
        }
        recipeEntry.setIngredients(temp.toString());
        return recipeEntry;
    }
}
